package com.example.edianzu.somur.module.mine.activity;

import android.os.Bundle;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.ListViewCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.edianzu.somur.R;
import com.example.edianzu.somur.api.constans.AppContents;
import com.example.edianzu.somur.api.manager.APIManager;
import com.example.edianzu.somur.module.mine.adapter.SampleListListViewAdapter;
import com.example.edianzu.somur.module.mine.bean.BoundSample;
import com.example.edianzu.somur.module.mine.bean.SampleList;
import com.example.edianzu.somur.utils.content.StatusBarUtils;
import com.example.edianzu.somur.utils.view.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class AddSampleActivity extends BaseActivity {

    @BindView(R.id.activity_addSample_back)
    AppCompatImageView activityAddSampleBack;
    @BindView(R.id.activity_addSample_ListView)
    ListViewCompat activityAddSampleListView;
    private int member_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.setWindowStatusBarColor(this, R.color.title);
        setContentView(R.layout.activity_add_sample);
        ButterKnife.bind(this);

        addFootView();

        member_id = getIntent().getIntExtra("member_id", 10);
        APIManager.getApiManagerInstance().getSampleList(new Observer<SampleList>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onNext(@NonNull SampleList sampleList) {

                if (sampleList.getStatus() == AppContents.STATUS) {

                    SampleListListViewAdapter adapter = new SampleListListViewAdapter(getApplicationContext());
                    adapter.add(sampleList.getData());

                    activityAddSampleListView.setAdapter(adapter);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {

                Toast.makeText(AddSampleActivity.this, "样本编号错误，请重新输入！！！", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete() {

            }
        }, member_id + "");
    }

    private void addFootView() {
        View footView = LayoutInflater.from(this).inflate(R.layout.item_sample_footview, null);
        FootViewHolder footViewHolder = new FootViewHolder(footView);
        activityAddSampleListView.addFooterView(footView);
    }

    @OnClick({R.id.activity_addSample_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_addSample_back:

                finish();
                break;
        }
    }

    class FootViewHolder {
        @BindView(R.id.activity_addSample_number)
        EditText activityAddSampleNumber;
        @BindView(R.id.activity_personalCenter_alter)
        Button activityPersonalCenterAlter;

        FootViewHolder(View view) {
            ButterKnife.bind(this, view);

            activityPersonalCenterAlter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final String sample_sn = activityAddSampleNumber.getText().toString();

                    if (!TextUtils.isEmpty(sample_sn)) {

                        APIManager.getApiManagerInstance().getBoundSample(new Observer<BoundSample>() {
                            @Override
                            public void onSubscribe(@NonNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NonNull BoundSample boundSample) {

                                if (boundSample.getStatus() == AppContents.STATUS) {

                                    Toast.makeText(AddSampleActivity.this, boundSample.getMsg(), Toast.LENGTH_SHORT).show();
                                    finish();
                                } else {

                                    Toast.makeText(AddSampleActivity.this, "样本编号错误，请重新输入！！！", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(@NonNull Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        }, sample_sn, member_id + "");
                    } else {

                        Toast.makeText(AddSampleActivity.this, "样本编号为空，请重新输入！！！", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}
